package com.irdstudio.tdp.console.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.alibaba.fastjson.JSON;
import com.irdstudio.tdp.console.dao.BpmBaseInfoDao;
import com.irdstudio.tdp.console.dao.BpmLinkedInfoDao;
import com.irdstudio.tdp.console.dao.BpmNodeInfoDao;
import com.irdstudio.tdp.console.dao.domain.BpmBaseInfo;
import com.irdstudio.tdp.console.dao.domain.BpmLinkedInfo;
import com.irdstudio.tdp.console.dao.domain.BpmNodeInfo;
import com.irdstudio.tdp.console.service.facade.BpmBaseInfoService;
import com.irdstudio.tdp.console.service.vo.BpmBaseInfoVO;
import com.irdstudio.tdp.console.service.vo.BpmNodeInfoVO;
import com.irdstudio.sdk.beans.core.util.KeyUtil;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.irdstudio.sdk.beans.core.base.FrameworkService;
/**
 * <p>ServiceImpl: 流程基本信息				<p>
 * @author srvadmin
 * @date 2020-07-24
 */
@Service("bpmBaseInfoServiceImpl")
public class BpmBaseInfoServiceImpl implements BpmBaseInfoService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(BpmBaseInfoServiceImpl.class);

	@Autowired
	private BpmBaseInfoDao bpmBaseInfoDao;

	@Autowired
	private BpmNodeInfoDao bpmNodeInfoDao;

	@Autowired
	private BpmLinkedInfoDao bpmLinkedInfoDao;
	/**
	 * 新增操作
	 */
	public int insertBpmBaseInfo(BpmBaseInfoVO inBpmBaseInfoVo) {
		logger.debug("当前新增数据为:"+ inBpmBaseInfoVo.toString());
		int num = 0;
		try {
            inBpmBaseInfoVo.setBpmId(KeyUtil.createUUIDKey());
			BpmBaseInfo bpmBaseInfo = new BpmBaseInfo();
			beanCopy(inBpmBaseInfoVo, bpmBaseInfo);
			num = bpmBaseInfoDao.insertBpmBaseInfo(bpmBaseInfo);
			if (num > 0) {
                // 初始化一个起始节点与结束节点，
                BpmNodeInfo startNode = new BpmNodeInfo();
                startNode.setBpmId(inBpmBaseInfoVo.getBpmId());
                startNode.setBpmNodeId(KeyUtil.createUUIDKey());
                startNode.setBpmNodeType("N01");
                startNode.setBpmNodeName("开始节点");
                startNode.setH(35);
                startNode.setW(200);
                startNode.setX(215);
                startNode.setY(25);
                bpmNodeInfoDao.insertBpmNodeInfo(startNode);

                BpmNodeInfo endNode = new BpmNodeInfo();
                endNode.setBpmId(inBpmBaseInfoVo.getBpmId());
                endNode.setBpmNodeId(KeyUtil.createUUIDKey());
                endNode.setBpmNodeType("N99");
                endNode.setBpmNodeName("结束节点");
                endNode.setH(35);
                endNode.setW(200);
                endNode.setX(215);
                endNode.setY(startNode.getY() + 100);
                bpmNodeInfoDao.insertBpmNodeInfo(endNode);
            }
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(BpmBaseInfoVO inBpmBaseInfoVo) {
		logger.debug("当前删除数据条件为:"+ inBpmBaseInfoVo);
		int num = 0;
		try {
			BpmBaseInfo bpmBaseInfo = new BpmBaseInfo();
			beanCopy(inBpmBaseInfoVo, bpmBaseInfo);
			num = bpmBaseInfoDao.deleteByPk(bpmBaseInfo);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inBpmBaseInfoVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(BpmBaseInfoVO inBpmBaseInfoVo) {
		logger.debug("当前修改数据为:"+ inBpmBaseInfoVo.toString());
		int num = 0;
		try {
			BpmBaseInfo bpmBaseInfo = new BpmBaseInfo();
			beanCopy(inBpmBaseInfoVo, bpmBaseInfo);
			num = bpmBaseInfoDao.updateByPk(bpmBaseInfo);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inBpmBaseInfoVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public BpmBaseInfoVO queryByPk(BpmBaseInfoVO inBpmBaseInfoVo) {
		
		logger.debug("当前查询参数信息为:"+ inBpmBaseInfoVo);
		try {
			BpmBaseInfo queryBpmBaseInfo = new BpmBaseInfo();
			beanCopy(inBpmBaseInfoVo, queryBpmBaseInfo);
			BpmBaseInfo queryRslBpmBaseInfo = bpmBaseInfoDao.queryByPk(queryBpmBaseInfo);
			if (Objects.nonNull(queryRslBpmBaseInfo)) {
				BpmBaseInfoVO outBpmBaseInfoVo = beanCopy(queryRslBpmBaseInfo, new BpmBaseInfoVO());
				logger.debug("当前查询结果为:"+ outBpmBaseInfoVo.toString());
				return outBpmBaseInfoVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BpmBaseInfoVO> queryAllOwner(BpmBaseInfoVO bpmBaseInfoVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<BpmBaseInfoVO> list = null;
		try {
			List<BpmBaseInfo> bpmBaseInfos = bpmBaseInfoDao.queryAllOwnerByPage(bpmBaseInfoVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ bpmBaseInfos.size());
			pageSet(bpmBaseInfos, bpmBaseInfoVo);
			list = (List<BpmBaseInfoVO>) beansCopy(bpmBaseInfos, BpmBaseInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BpmBaseInfoVO> queryAllCurrOrg(BpmBaseInfoVO bpmBaseInfoVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<BpmBaseInfo> bpmBaseInfos = bpmBaseInfoDao.queryAllCurrOrgByPage(bpmBaseInfoVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+bpmBaseInfos.size());
		List<BpmBaseInfoVO> list = null;
		try {
			pageSet(bpmBaseInfos, bpmBaseInfoVo);
			list = (List<BpmBaseInfoVO>) beansCopy(bpmBaseInfos, BpmBaseInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}

	@Override
	public Map<String, Object> queryBpmBaseInfoFlowData(String bpmId) {
		Map<String, Object> flowData = new HashMap<>();

		BpmBaseInfo queryBpmBaseInfo = new BpmBaseInfo();
		queryBpmBaseInfo.setBpmId(bpmId);
		try {
			BpmBaseInfo bpmBaseInfo = bpmBaseInfoDao.queryByPk(queryBpmBaseInfo);
			if (bpmBaseInfo != null) {
				BpmNodeInfoVO nodeParam = new BpmNodeInfoVO();
				nodeParam.setBpmId(bpmId);
				List<BpmNodeInfo> bpmNodeInfos = bpmNodeInfoDao.queryBpmNodeInfoList(nodeParam);

				List<BpmLinkedInfo> bpmLinkeds = bpmLinkedInfoDao.queryBpmLinkedInfosByBpmId(bpmId);

				flowData.put(BpmBaseInfo.class.getSimpleName(), bpmBaseInfo);
				flowData.put(BpmNodeInfo.class.getSimpleName(), bpmNodeInfos);
				flowData.put(BpmLinkedInfo.class.getSimpleName(), bpmLinkeds);
			}
		} catch (Exception e) {
			logger.error("查询流程配置异常！{}", e);
			return null;
		}
		return flowData;
	}

	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BpmBaseInfoVO> queryAllCurrDownOrg(BpmBaseInfoVO bpmBaseInfoVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<BpmBaseInfo> bpmBaseInfos = bpmBaseInfoDao.queryAllCurrDownOrgByPage(bpmBaseInfoVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ bpmBaseInfos.size());
		List<BpmBaseInfoVO> list = null;
		try {
			pageSet(bpmBaseInfos, bpmBaseInfoVo);
			list = (List<BpmBaseInfoVO>) beansCopy(bpmBaseInfos, BpmBaseInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}

	@Override
	public Boolean saveBpmBaseInfoFlowData(Map<String, Object> flowData) {

		List nodeInfos = (List) flowData.get(BpmNodeInfo.class.getSimpleName());
		List linkeds = (List) flowData.get(BpmLinkedInfo.class.getSimpleName());

		if (CollectionUtils.isNotEmpty(nodeInfos) ) {
			// 转换成 List of BpmNodeInfo
			List<BpmNodeInfo> nodeList = JSON.parseArray(JSON.toJSONString(nodeInfos), BpmNodeInfo.class);
			for (BpmNodeInfo node : nodeList) {

				try {
					BpmNodeInfo checkObj = bpmNodeInfoDao.queryByPk(node);
					if (checkObj != null) {
						bpmNodeInfoDao.updateByPk(node);
					} else {
						bpmNodeInfoDao.insertBpmNodeInfo(node);
					}
				} catch (Exception e) {
					logger.error("", e);
					throw new RuntimeException(e.getMessage(), e);
				}

			}
		}
		if (CollectionUtils.isNotEmpty(linkeds) ) {
			// 转换成 List of BpmNodeInfo
			List<BpmLinkedInfo> linkedList = JSON.parseArray(JSON.toJSONString(linkeds), BpmLinkedInfo.class);
			for (BpmLinkedInfo linked : linkedList) {

				try {
					BpmLinkedInfo checkObj = bpmLinkedInfoDao.queryByPk(linked);
					if (checkObj != null) {
						bpmLinkedInfoDao.updateByPk(linked);
					} else {
						bpmLinkedInfoDao.insertBpmLinkedInfo(linked);
					}
				} catch (Exception e) {
					logger.error("", e);
					throw new RuntimeException(e.getMessage(), e);
				}

			}
		}
		return true;
	}
}
